package com.pp.ent.util;

/**
 * Created by dengqu on 2017/3/2.
 */

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class ResolutionUtils
{
    public static int getScreenWidth(Context context)
    {
        return getScreenSize(context, null).x;
    }

    public static int getScreenHeight(Context context)
    {
        return getScreenSize(context, null).y;
    }

    public static Point getScreenSize(Context context, Point outSize)
    {
        WindowManager wm = (WindowManager)context.getSystemService("window");

        Point ret = outSize == null ? new Point() : outSize;
        Display defaultDisplay = wm.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= 13)
        {
            defaultDisplay.getSize(ret);
        }
        else
        {
            ret.x = defaultDisplay.getWidth();
            ret.y = defaultDisplay.getHeight();
        }
        return ret;
    }

    public static float convertDpToPixel(float dp, Context context)
    {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160.0F);
        return px;
    }

    public static float convertPixelsToDp(float px, Context context)
    {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160.0F);
        return dp;
    }

    public static float getScreenWidthHeightRatio(Context context, Point outSize)
    {
        WindowManager wm = (WindowManager)context.getSystemService("window");

        Point ret = outSize == null ? new Point() : outSize;
        Display defaultDisplay = wm.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= 13)
        {
            defaultDisplay.getSize(ret);
        }
        else
        {
            ret.x = defaultDisplay.getWidth();
            ret.y = defaultDisplay.getHeight();
        }
        float xf = ret.x;
        float yf = ret.y;
        return xf / yf;
    }
}

